{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "c9060960-f8c6-4c09-b808-16ca4eaf6be4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2.11.0'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import tensorflow as tf\n",
    "from tensorflow import keras\n",
    "from tensorflow.keras.models import *\n",
    "from tensorflow.keras.layers import *\n",
    "import regex as re\n",
    "import scipy as sp\n",
    "from tensorflow.keras.utils import to_categorical\n",
    "tf.__version__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "5a479c6c-ef3d-4b1e-8b2f-0f8618e189ce",
   "metadata": {},
   "outputs": [],
   "source": [
    "from multiprocessing import Pool"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "15f78d15-c2c2-4dc8-8006-fc2487cd60dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os \n",
    "os.environ['CUDA_VISIBLE_DEVICES']='0'\n",
    "import tensorflow as tf\n",
    "gpu_list = tf.config.experimental.list_physical_devices('GPU')\n",
    "if len(gpu_list) > 0 :\n",
    "    for gpu in gpu_list:\n",
    "        try:\n",
    "            tf.config.experimental.set_memory_growth(gpu, True)\n",
    "            print(gpu)\n",
    "        except RuntimeError as e:\n",
    "            print(e)\n",
    "else:\n",
    "\tprint(\"Got no GPUs\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "6526825f-7eae-483e-bee5-4ae9f01ff5bb",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('test.txt') as f:\n",
    "    test = f.read()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7f3a3f77-21f2-4c5c-86af-6f828000a85c",
   "metadata": {},
   "outputs": [],
   "source": [
    "def preprocessing_seq(fasta_text):\n",
    "    name_lst = []\n",
    "    seq_lst = []\n",
    "    str_lst = fasta_text.splitlines(keepends=False)\n",
    "    for i in range(len(str_lst)-1):\n",
    "        if str_lst[i][0] == '>':\n",
    "            seq = str_lst[i+1].upper()\n",
    "            seq = seq.replace('U','T')\n",
    "            if len(seq) > 100:\n",
    "                seq = seq[-100:]\n",
    "            if len(seq) >= 25:\n",
    "                seq_lst.append(seq)\n",
    "                name_lst.append(str_lst[i][1:])\n",
    "    return seq_lst, name_lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d0a31cd1-21c0-4360-85f7-a3f6aeece13f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "64173d0c-a997-44e5-8eef-08596dff0d4d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['> seq1',\n",
       " 'GACGGTGAGAATGTCTGGCTTTGCGTCATTGATGGACGGCATGATGGGTTTGAGCGAGTCCTGCGGTCCGGGTTACGGTATGTGGGGTAGGG',\n",
       " '> seq2',\n",
       " 'CCCAAGTAGCAATGCGGAACATATCTAATTTCGGAATGAAGAGCAACTACC',\n",
       " '> seq3',\n",
       " 'CAAAGATGTGAGGGTAGGACCAACAAGCCGACTGACCCCAGGAATGGGGACAATCTAGTTCCAGCTCAGCGGC',\n",
       " '> seq4',\n",
       " 'TCAATGGACACATTAACTTCACCAAGATGACTACTCGATCAAAAA',\n",
       " '> seq5',\n",
       " 'GTCAGTTATCCCGCGACATAATGGAGAATGCTAACGCCATTTTCCCTACCCGAATACACCTACCCATTGAGTAGCTGGG',\n",
       " '> seq6',\n",
       " 'GCCCGACCATGCCGGCACCACCCAAACACCTACACCGCCCTCACATACTCTGGAAGGCACATGTGAGCGCAG',\n",
       " '> seq7',\n",
       " 'GTATCGATGGATGCTATATATCCGTTACCCGCTACCGTGGGGGCCGT',\n",
       " '> seq8',\n",
       " 'AAGACGAGGCAA',\n",
       " '> seq9']"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_lst = []\n",
    "seq_lst = []\n",
    "str_lst = test.splitlines(keepends=False)\n",
    "str_lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "b4678125-5d39-46b3-ac77-5023b13a831d",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(len(str_lst)-1):\n",
    "    if str_lst[i][0] == '>':\n",
    "        seq = str_lst[i+1].upper()\n",
    "        seq = seq.replace('U','T')\n",
    "        if len(seq) > 100:\n",
    "            seq = seq[-100:]\n",
    "        if len(seq) >= 25:\n",
    "            seq_lst.append(seq)\n",
    "            name_lst.append(str_lst[i][1:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "403bf2b4-76d2-4efb-b223-4f6a9ffdda46",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['GACGGTGAGAATGTCTGGCTTTGCGTCATTGATGGACGGCATGATGGGTTTGAGCGAGTCCTGCGGTCCGGGTTACGGTATGTGGGGTAGGG', 'CCCAAGTAGCAATGCGGAACATATCTAATTTCGGAATGAAGAGCAACTACC', 'CAAAGATGTGAGGGTAGGACCAACAAGCCGACTGACCCCAGGAATGGGGACAATCTAGTTCCAGCTCAGCGGC', 'TCAATGGACACATTAACTTCACCAAGATGACTACTCGATCAAAAA', 'GTCAGTTATCCCGCGACATAATGGAGAATGCTAACGCCATTTTCCCTACCCGAATACACCTACCCATTGAGTAGCTGGG', 'GCCCGACCATGCCGGCACCACCCAAACACCTACACCGCCCTCACATACTCTGGAAGGCACATGTGAGCGCAG', 'GTATCGATGGATGCTATATATCCGTTACCCGCTACCGTGGGGGCCGT']\n",
      "[' seq1', ' seq2', ' seq3', ' seq4', ' seq5', ' seq6', ' seq7']\n"
     ]
    }
   ],
   "source": [
    "print(seq_lst)\n",
    "print(name_lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7c16ca76-8452-4c51-8375-7c5d886e208e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def seq2oh(seq):\n",
    "    vocab = {\n",
    "        'A': [1,0,0,0,0],\n",
    "        'T': [0,1,0,0,0],\n",
    "        'C': [0,0,1,0,0],\n",
    "        'G': [0,0,0,1,0],\n",
    "        '_': [0,0,0,0,1]\n",
    "    }\n",
    "    if len(seq)<100:\n",
    "        seq = '_' * (100-len(seq)) + seq\n",
    "    oh = []\n",
    "    for item in seq:\n",
    "        oh.append(vocab[item])\n",
    "    return np.array(oh)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f6ad23f8-6d4a-4b24-88b7-115896f03f39",
   "metadata": {},
   "outputs": [],
   "source": [
    "def oh2seq(oh):\n",
    "    seq = ''\n",
    "    vocab = {\n",
    "        'A': [1,0,0,0,0],\n",
    "        'T': [0,1,0,0,0],\n",
    "        'C': [0,0,1,0,0],\n",
    "        'G': [0,0,0,1,0],\n",
    "        '_': [0,0,0,0,1]\n",
    "    }\n",
    "    for item in oh:\n",
    "        char = list(vocab.keys())[list(vocab.values()).index(list(item))]\n",
    "        seq += char\n",
    "    seq = seq.split('_')[-1]\n",
    "    return seq"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "13a9b69a-2b46-4bbe-a075-e1380a70e536",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(7, 100, 5)\n"
     ]
    }
   ],
   "source": [
    "seq_oharr = []\n",
    "for item in seq_lst:\n",
    "    seq_f = seq2oh(item)\n",
    "    seq_oharr.append(seq_f)\n",
    "seq_oharr = np.array(seq_oharr)\n",
    "print(seq_oharr.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "c18c5f54-8f5c-4758-aebc-c36cb45a127e",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2024-03-22 03:06:20.803358: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  SSE4.1 SSE4.2 AVX AVX2 AVX512F AVX512_VNNI FMA\n",
      "To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n",
      "2024-03-22 03:06:22.941389: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1613] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 60858 MB memory:  -> device: 0, name: NVIDIA A100 80GB PCIe, pci bus id: 0000:5b:00.0, compute capability: 8.0\n"
     ]
    }
   ],
   "source": [
    "eval_model_25 = load_model('models/CGRU_25_100.hd5')\n",
    "eval_model_50 = load_model('models/CGRU_50.hd5')\n",
    "gen_model = load_model('models/UTR_genAE_CGRU_25_100_compiled.hd5')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4df1cf58-1751-4267-8c01-69eae8c9ee36",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "e2cf2570-3047-4efa-a74c-a9213e04a764",
   "metadata": {},
   "outputs": [],
   "source": [
    "# test_50 = ['CTAGCTCCGTGCGCTAAAATAGGCTGCGATGTCAGCAACGGTGGAGACCA',\n",
    "#            'AAAAAAAAAACTAGCTCCGTGCGCTAAAATAGGCTGCGATGTCAGCAACGGTGGAGACCA',\n",
    "#            'TTTTTTTTTTCTAGCTCCGTGCGCTAAAATAGGCTGCGATGTCAGCAACGGTGGAGACCA',\n",
    "#            'CCCCCCCCCCCTAGCTCCGTGCGCTAAAATAGGCTGCGATGTCAGCAACGGTGGAGACCA',\n",
    "#            'GGGGGGGGGGCTAGCTCCGTGCGCTAAAATAGGCTGCGATGTCAGCAACGGTGGAGACCA']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "f1a3a6ac-6741-4046-9d10-bf6b4adb2186",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(5, 100, 5)\n"
     ]
    }
   ],
   "source": [
    "# seq_oh50 = []\n",
    "# for item in test_50:\n",
    "#     seq_f = seq2oh(item)\n",
    "#     seq_oh50.append(seq_f)\n",
    "# seq_oh50 = np.array(seq_oh50)\n",
    "# print(seq_oh50.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "9c24c2e5-528d-4865-a37b-36d59bb2fca1",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2024-03-18 01:58:37.118803: I tensorflow/compiler/xla/stream_executor/cuda/cuda_dnn.cc:428] Loaded cuDNN version 8800\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1/1 [==============================] - 8s 8s/step\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Could not load symbol cublasGetSmCountTarget from libcublas.so.11. Error: /home/liuyu/miniconda3/envs/deepl/lib/python3.9/site-packages/tensorflow/python/../../../../libcublas.so.11: undefined symbol: cublasGetSmCountTarget\n",
      "2024-03-18 01:58:42.452620: I tensorflow/compiler/xla/stream_executor/cuda/cuda_blas.cc:630] TensorFloat-32 will be used for the matrix multiplication. This will only be logged once.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([3.6366057, 3.8408494, 3.9413614, 3.481472 , 3.4667487],\n",
       "      dtype=float32)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# pred_mrl_50 = eval_model_50.predict(seq_oh50).flatten()\n",
    "# pred_mrl_50"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "68765d61-140b-4db9-a0a1-7ac37ccdfd74",
   "metadata": {},
   "outputs": [],
   "source": [
    "def predict_rl(text_fa):\n",
    "    seq_lst, name_lst = preprocessing_seq(text_fa)\n",
    "    seq_oharr = []\n",
    "    for item in seq_lst:\n",
    "        seq_f = seq2oh(item)\n",
    "        seq_oharr.append(seq_f)\n",
    "    seq_oharr = np.array(seq_oharr)\n",
    "    eval_model_25 = load_model('models/CGRU_25_100.hd5')\n",
    "    eval_model_50 = load_model('models/CGRU_50.hd5')\n",
    "    gen_model = load_model('models/UTR_genAE_CGRU_25_100_compiled.hd5')\n",
    "    opt_lst = []\n",
    "    opt_oh_lst = []\n",
    "    pred_mrl_ori = eval_model_25.predict(seq_oharr, verbose=0).flatten()\n",
    "    seq_ae = gen_model.predict(seq_oharr, verbose=0)\n",
    "    for i in range(len(seq_lst)):\n",
    "        ae_arg = np.argmax(seq_ae[i], axis=-1)\n",
    "        ae_argoh = to_categorical(ae_arg, 5)\n",
    "        for m in range(100-len(seq_lst[i]),100):\n",
    "            if ae_argoh[m][4] == 1:\n",
    "                ae_argoh[m] = np.array([0,0,1,0,0])\n",
    "        seq_nat = oh2seq(ae_argoh)\n",
    "        seq_nat_opt = ''\n",
    "        for j in range(len(seq_lst[i])):\n",
    "            try:\n",
    "                if seq_lst[i][j] != seq_nat[j]:\n",
    "                    seq_nat_opt = seq_nat_opt + seq_nat[j].lower()\n",
    "                else:\n",
    "                    seq_nat_opt = seq_nat_opt + seq_nat[j]\n",
    "            except:\n",
    "                pass\n",
    "        opt_oh_lst.append(ae_argoh)\n",
    "        opt_lst.append(seq_nat_opt)\n",
    "    pred_mrl_opt = eval_model_25.predict(np.array(opt_oh_lst), verbose=0).flatten()\n",
    "    feat_arr_50 = np.array(opt_oh_lst)\n",
    "    feat_arr_50[:,:50] = np.array([0, 0, 0, 0, 1])\n",
    "    pred_mrl_opt_50 = eval_model_50.predict(np.array(feat_arr_50), verbose=0).flatten()\n",
    "    inc_mrl = pred_mrl_opt - pred_mrl_ori\n",
    "    df_result = pd.DataFrame({'Sequence name': name_lst,\n",
    "                              'Original UTR': seq_lst, \n",
    "                              'Original RL': pred_mrl_ori, \n",
    "                              'Optimized UTR': opt_lst,\n",
    "                              'Optimized RL': pred_mrl_opt, \n",
    "                              'Optimized RL_50nt': pred_mrl_opt_50,\n",
    "                              'Increased RL': inc_mrl, \n",
    "                             })\n",
    "    return df_result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "da5a37e8-12c8-4464-a91a-405f60eb666c",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2024-03-18 03:53:12.238017: I tensorflow/compiler/xla/stream_executor/cuda/cuda_dnn.cc:428] Loaded cuDNN version 8800\n",
      "Could not load symbol cublasGetSmCountTarget from libcublas.so.11. Error: /home/liuyu/miniconda3/envs/deepl/lib/python3.9/site-packages/tensorflow/python/../../../../libcublas.so.11: undefined symbol: cublasGetSmCountTarget\n",
      "2024-03-18 03:53:15.005737: I tensorflow/compiler/xla/stream_executor/cuda/cuda_blas.cc:630] TensorFloat-32 will be used for the matrix multiplication. This will only be logged once.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Sequence name</th>\n",
       "      <th>Original UTR</th>\n",
       "      <th>Original RL</th>\n",
       "      <th>Optimized UTR</th>\n",
       "      <th>Optimized RL</th>\n",
       "      <th>Optimized RL_50nt</th>\n",
       "      <th>Increased RL</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>seq1</td>\n",
       "      <td>GACGGTGAGAATGTCTGGCTTTGCGTCATTGATGGACGGCATGATG...</td>\n",
       "      <td>2.027970</td>\n",
       "      <td>GACGGTGAGAATtTCTGGCTTTGCGTCATTGATtGACGGCAgGATt...</td>\n",
       "      <td>7.088054</td>\n",
       "      <td>7.705301</td>\n",
       "      <td>5.060084</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>seq2</td>\n",
       "      <td>CCCAAGTAGCAATGCGGAACATATCTAATTTCGGAATGAAGAGCAA...</td>\n",
       "      <td>2.270549</td>\n",
       "      <td>ggCAAGTAGCAATtCGGAACATATCTAATTTCGGAATtAAGAGCAg...</td>\n",
       "      <td>6.788652</td>\n",
       "      <td>8.098377</td>\n",
       "      <td>4.518103</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>seq3</td>\n",
       "      <td>CAAAGATGTGAGGGTAGGACCAACAAGCCGACTGACCCCAGGAATG...</td>\n",
       "      <td>2.365892</td>\n",
       "      <td>tgAAGATtTGAGGGTAGGACCAACAAGCCGACTGACCCCAGGAATt...</td>\n",
       "      <td>6.020560</td>\n",
       "      <td>7.776721</td>\n",
       "      <td>3.654668</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>seq4</td>\n",
       "      <td>TCAATGGACACATTAACTTCACCAAGATGACTACTCGATCAAAAA</td>\n",
       "      <td>2.453437</td>\n",
       "      <td>TCAATtGACACATTtACTTCACgAAGATtACTACTCGATCgcAAg</td>\n",
       "      <td>6.788006</td>\n",
       "      <td>8.089871</td>\n",
       "      <td>4.334569</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>seq5</td>\n",
       "      <td>GTCAGTTATCCCGCGACATAATGGAGAATGCTAACGCCATTTTCCC...</td>\n",
       "      <td>2.416512</td>\n",
       "      <td>GTCAGTTATCCCGCGACATAATtGAGAAgGCTAACGCCATTTTCCC...</td>\n",
       "      <td>6.727188</td>\n",
       "      <td>7.914410</td>\n",
       "      <td>4.310676</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>seq6</td>\n",
       "      <td>GCCCGACCATGCCGGCACCACCCAAACACCTACACCGCCCTCACAT...</td>\n",
       "      <td>2.134278</td>\n",
       "      <td>GCCCGACCATtCCGGCACCACgCAAACACCTACACCGCaCTCACAT...</td>\n",
       "      <td>5.739672</td>\n",
       "      <td>5.783462</td>\n",
       "      <td>3.605394</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>seq7</td>\n",
       "      <td>GTATCGATGGATGCTATATATCCGTTACCCGCTACCGTGGGGGCCGT</td>\n",
       "      <td>2.491951</td>\n",
       "      <td>GTATCGATtGATtCTATATATCCGTTACCCGCTACCGTGGGGGCgGT</td>\n",
       "      <td>6.418518</td>\n",
       "      <td>7.679404</td>\n",
       "      <td>3.926567</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  Sequence name                                       Original UTR  \\\n",
       "0          seq1  GACGGTGAGAATGTCTGGCTTTGCGTCATTGATGGACGGCATGATG...   \n",
       "1          seq2  CCCAAGTAGCAATGCGGAACATATCTAATTTCGGAATGAAGAGCAA...   \n",
       "2          seq3  CAAAGATGTGAGGGTAGGACCAACAAGCCGACTGACCCCAGGAATG...   \n",
       "3          seq4      TCAATGGACACATTAACTTCACCAAGATGACTACTCGATCAAAAA   \n",
       "4          seq5  GTCAGTTATCCCGCGACATAATGGAGAATGCTAACGCCATTTTCCC...   \n",
       "5          seq6  GCCCGACCATGCCGGCACCACCCAAACACCTACACCGCCCTCACAT...   \n",
       "6          seq7    GTATCGATGGATGCTATATATCCGTTACCCGCTACCGTGGGGGCCGT   \n",
       "\n",
       "   Original RL                                      Optimized UTR  \\\n",
       "0     2.027970  GACGGTGAGAATtTCTGGCTTTGCGTCATTGATtGACGGCAgGATt...   \n",
       "1     2.270549  ggCAAGTAGCAATtCGGAACATATCTAATTTCGGAATtAAGAGCAg...   \n",
       "2     2.365892  tgAAGATtTGAGGGTAGGACCAACAAGCCGACTGACCCCAGGAATt...   \n",
       "3     2.453437      TCAATtGACACATTtACTTCACgAAGATtACTACTCGATCgcAAg   \n",
       "4     2.416512  GTCAGTTATCCCGCGACATAATtGAGAAgGCTAACGCCATTTTCCC...   \n",
       "5     2.134278  GCCCGACCATtCCGGCACCACgCAAACACCTACACCGCaCTCACAT...   \n",
       "6     2.491951    GTATCGATtGATtCTATATATCCGTTACCCGCTACCGTGGGGGCgGT   \n",
       "\n",
       "   Optimized RL  Optimized RL_50nt  Increased RL  \n",
       "0      7.088054           7.705301      5.060084  \n",
       "1      6.788652           8.098377      4.518103  \n",
       "2      6.020560           7.776721      3.654668  \n",
       "3      6.788006           8.089871      4.334569  \n",
       "4      6.727188           7.914410      4.310676  \n",
       "5      5.739672           5.783462      3.605394  \n",
       "6      6.418518           7.679404      3.926567  "
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res_df = predict_rl(test)\n",
    "res_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "0a871afe-e557-4016-ab36-e47b9a5c2974",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> seq1\n",
      "GACGGTGAGAATGTCTGGCTTTGCGTCATTGATGGACGGCATGATGGGTTTGAGCGAGTCCTGCGGTCCGGGTTACGGTATGTGGGGTAGGG\n",
      "> seq2\n",
      "CCCAAGTAGCAATGCGGAACATATCTAATTTCGGAATGAAGAGCAACTACC\n",
      "> seq3\n",
      "CAAAGATGTGAGGGTAGGACCAACAAGCCGACTGACCCCAGGAATGGGGACAATCTAGTTCCAGCTCAGCGGC\n",
      "> seq4\n",
      "TCAATGGACACATTAACTTCACCAAGATGACTACTCGATCAAAAA\n",
      "> seq5\n",
      "GTCAGTTATCCCGCGACATAATGGAGAATGCTAACGCCATTTTCCCTACCCGAATACACCTACCCATTGAGTAGCTGGG\n",
      "> seq6\n",
      "GCCCGACCATGCCGGCACCACCCAAACACCTACACCGCCCTCACATACTCTGGAAGGCACATGTGAGCGCAG\n",
      "> seq7\n",
      "GTATCGATGGATGCTATATATCCGTTACCCGCTACCGTGGGGGCCGT\n",
      "> seq8\n",
      "AAGACGAGGCAA\n",
      "> seq9\n"
     ]
    }
   ],
   "source": [
    "print(test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "4db2cf96-7046-4b80-acdc-23d4c0e9d68d",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2024-03-18 04:03:13.013659: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  SSE4.1 SSE4.2 AVX AVX2 AVX512F AVX512_VNNI FMA\n",
      "To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n",
      "2024-03-18 04:03:15.808502: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1613] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 13337 MB memory:  -> device: 0, name: NVIDIA A100 80GB PCIe, pci bus id: 0000:25:00.0, compute capability: 8.0\n",
      "2024-03-18 04:03:15.812337: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1613] Created device /job:localhost/replica:0/task:0/device:GPU:1 with 78908 MB memory:  -> device: 1, name: NVIDIA A100 80GB PCIe, pci bus id: 0000:5b:00.0, compute capability: 8.0\n",
      "2024-03-18 04:03:15.815599: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1613] Created device /job:localhost/replica:0/task:0/device:GPU:2 with 59188 MB memory:  -> device: 2, name: NVIDIA A100 80GB PCIe, pci bus id: 0000:9b:00.0, compute capability: 8.0\n",
      "2024-03-18 04:03:15.818913: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1613] Created device /job:localhost/replica:0/task:0/device:GPU:3 with 68492 MB memory:  -> device: 3, name: NVIDIA A100 80GB PCIe, pci bus id: 0000:c8:00.0, compute capability: 8.0\n",
      "2024-03-18 04:03:21.432479: I tensorflow/compiler/xla/stream_executor/cuda/cuda_dnn.cc:428] Loaded cuDNN version 8800\n",
      "Could not load symbol cublasGetSmCountTarget from libcublas.so.11. Error: /home/liuyu/miniconda3/envs/deepl/lib/python3.9/site-packages/tensorflow/python/../../../../libcublas.so.11: undefined symbol: cublasGetSmCountTarget\n",
      "2024-03-18 04:03:23.071680: I tensorflow/compiler/xla/stream_executor/cuda/cuda_blas.cc:630] TensorFloat-32 will be used for the matrix multiplication. This will only be logged once.\n"
     ]
    }
   ],
   "source": [
    "pool = Pool(processes=2)\n",
    "res_df = pool.apply(predict_rl, (test,))\n",
    "pool.close()\n",
    "pool.join()\n",
    "file_name = '{}_report.xlsx'.format('test')\n",
    "res_df.to_excel(file_name, index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0caf8885-4391-4d54-bb14-1176b1ba7bd3",
   "metadata": {},
   "outputs": [],
   "source": [
    "def output_result(form, pam=\"[ATCG]GG\", enzyme='wt', show_CFD=True, task_num=None):\n",
    "    pool = Pool(processes=2)\n",
    "    if enzyme not in ['wt','esp','hf']:\n",
    "        raise ValueError(\"Enzyme can only be 'wt','esp' or 'hf', please confirm the input is correct\")\n",
    "    seq_df = read_data(form, pam)\n",
    "    res_df = pool.apply(efficiency_predict,(seq_df, enzyme, show_CFD))\n",
    "    pool.close()\n",
    "    pool.join()\n",
    "    t = str(int(time.time()))\n",
    "    file_name = 'CRISep_app/static/output/{}_report.xlsx'.format(task_num)\n",
    "    res_df.to_excel(file_name, index=False)\n",
    "    print('Analysis completed! You can find output file in {}.'.format(file_name))\n",
    "    return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "2745e41c-69ab-44d7-9845-b28f2ef4b531",
   "metadata": {},
   "outputs": [],
   "source": [
    "opt_lst = []\n",
    "opt_oh_lst = []\n",
    "pred_mrl_ori = eval_model_25.predict(seq_oharr, verbose=0).flatten()\n",
    "seq_ae = gen_model.predict(seq_oharr, verbose=0)\n",
    "for i in range(len(seq_lst)):\n",
    "    ae_arg = np.argmax(seq_ae[i], axis=-1)\n",
    "    ae_argoh = to_categorical(ae_arg, 5)\n",
    "    for m in range(100-len(seq_lst[i]),100):\n",
    "        if ae_argoh[m][4] == 1:\n",
    "            ae_argoh[m] = np.array([0,0,1,0,0])\n",
    "    seq_nat = oh2seq(ae_argoh)\n",
    "    seq_nat_opt = ''\n",
    "    for j in range(len(seq_lst[i])):\n",
    "        try:\n",
    "            if seq_lst[i][j] != seq_nat[j]:\n",
    "                seq_nat_opt = seq_nat_opt + seq_nat[j].lower()\n",
    "            else:\n",
    "                seq_nat_opt = seq_nat_opt + seq_nat[j]\n",
    "        except:\n",
    "            pass\n",
    "    opt_oh_lst.append(ae_argoh)\n",
    "    opt_lst.append(seq_nat_opt)\n",
    "pred_mrl_opt = eval_model_25.predict(np.array(opt_oh_lst), verbose=0).flatten()\n",
    "feat_arr_50 = np.array(opt_oh_lst)\n",
    "feat_arr_50[:,:50] = np.array([0, 0, 0, 0, 1])\n",
    "pred_mrl_opt_50 = eval_model_50.predict(np.array(feat_arr_50), verbose=0).flatten()\n",
    "inc_mrl = pred_mrl_opt - pred_mrl_ori"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "31d9273a-fcba-491a-965c-801c1c1ce62f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Sequence name</th>\n",
       "      <th>Original UTR</th>\n",
       "      <th>Original RL</th>\n",
       "      <th>Optimized UTR</th>\n",
       "      <th>Optimized RL</th>\n",
       "      <th>Optimized RL_50nt</th>\n",
       "      <th>Increased RL</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>seq1</td>\n",
       "      <td>GACGGTGAGAATGTCTGGCTTTGCGTCATTGATGGACGGCATGATG...</td>\n",
       "      <td>2.027974</td>\n",
       "      <td>GACGGTGAGAATtTCTGGCTTTGCGTCATTGATtGACGGCAgGATt...</td>\n",
       "      <td>7.088026</td>\n",
       "      <td>7.705301</td>\n",
       "      <td>5.060052</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>seq2</td>\n",
       "      <td>CCCAAGTAGCAATGCGGAACATATCTAATTTCGGAATGAAGAGCAA...</td>\n",
       "      <td>2.270549</td>\n",
       "      <td>ggCAAGTAGCAATtCGGAACATATCTAATTTCGGAATtAAGAGCAg...</td>\n",
       "      <td>6.788651</td>\n",
       "      <td>8.098377</td>\n",
       "      <td>4.518102</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>seq3</td>\n",
       "      <td>CAAAGATGTGAGGGTAGGACCAACAAGCCGACTGACCCCAGGAATG...</td>\n",
       "      <td>2.365864</td>\n",
       "      <td>tgAAGATtTGAGGGTAGGACCAACAAGCCGACTGACCCCAGGAATt...</td>\n",
       "      <td>6.020535</td>\n",
       "      <td>7.776737</td>\n",
       "      <td>3.654671</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>seq4</td>\n",
       "      <td>TCAATGGACACATTAACTTCACCAAGATGACTACTCGATCAAAAA</td>\n",
       "      <td>2.453431</td>\n",
       "      <td>TCAATtGACACATTtACTTCACgAAGATtACTACTCGATCgcAAg</td>\n",
       "      <td>6.787998</td>\n",
       "      <td>8.089881</td>\n",
       "      <td>4.334567</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>seq5</td>\n",
       "      <td>GTCAGTTATCCCGCGACATAATGGAGAATGCTAACGCCATTTTCCC...</td>\n",
       "      <td>2.416500</td>\n",
       "      <td>GTCAGTTATCCCGCGACATAATtGAGAAgGCTAACGCCATTTTCCC...</td>\n",
       "      <td>6.727188</td>\n",
       "      <td>7.914385</td>\n",
       "      <td>4.310688</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>seq6</td>\n",
       "      <td>GCCCGACCATGCCGGCACCACCCAAACACCTACACCGCCCTCACAT...</td>\n",
       "      <td>2.134270</td>\n",
       "      <td>GCCCGACCATtCCGGCACCACgCAAACACCTACACCGCaCTCACAT...</td>\n",
       "      <td>5.739672</td>\n",
       "      <td>5.783389</td>\n",
       "      <td>3.605402</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>seq7</td>\n",
       "      <td>GTATCGATGGATGCTATATATCCGTTACCCGCTACCGTGGGGGCCGT</td>\n",
       "      <td>2.491951</td>\n",
       "      <td>GTATCGATtGATtCTATATATCCGTTACCCGCTACCGTGGGGGCgGT</td>\n",
       "      <td>6.418518</td>\n",
       "      <td>7.679404</td>\n",
       "      <td>3.926567</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  Sequence name                                       Original UTR  \\\n",
       "0          seq1  GACGGTGAGAATGTCTGGCTTTGCGTCATTGATGGACGGCATGATG...   \n",
       "1          seq2  CCCAAGTAGCAATGCGGAACATATCTAATTTCGGAATGAAGAGCAA...   \n",
       "2          seq3  CAAAGATGTGAGGGTAGGACCAACAAGCCGACTGACCCCAGGAATG...   \n",
       "3          seq4      TCAATGGACACATTAACTTCACCAAGATGACTACTCGATCAAAAA   \n",
       "4          seq5  GTCAGTTATCCCGCGACATAATGGAGAATGCTAACGCCATTTTCCC...   \n",
       "5          seq6  GCCCGACCATGCCGGCACCACCCAAACACCTACACCGCCCTCACAT...   \n",
       "6          seq7    GTATCGATGGATGCTATATATCCGTTACCCGCTACCGTGGGGGCCGT   \n",
       "\n",
       "   Original RL                                      Optimized UTR  \\\n",
       "0     2.027974  GACGGTGAGAATtTCTGGCTTTGCGTCATTGATtGACGGCAgGATt...   \n",
       "1     2.270549  ggCAAGTAGCAATtCGGAACATATCTAATTTCGGAATtAAGAGCAg...   \n",
       "2     2.365864  tgAAGATtTGAGGGTAGGACCAACAAGCCGACTGACCCCAGGAATt...   \n",
       "3     2.453431      TCAATtGACACATTtACTTCACgAAGATtACTACTCGATCgcAAg   \n",
       "4     2.416500  GTCAGTTATCCCGCGACATAATtGAGAAgGCTAACGCCATTTTCCC...   \n",
       "5     2.134270  GCCCGACCATtCCGGCACCACgCAAACACCTACACCGCaCTCACAT...   \n",
       "6     2.491951    GTATCGATtGATtCTATATATCCGTTACCCGCTACCGTGGGGGCgGT   \n",
       "\n",
       "   Optimized RL  Optimized RL_50nt  Increased RL  \n",
       "0      7.088026           7.705301      5.060052  \n",
       "1      6.788651           8.098377      4.518102  \n",
       "2      6.020535           7.776737      3.654671  \n",
       "3      6.787998           8.089881      4.334567  \n",
       "4      6.727188           7.914385      4.310688  \n",
       "5      5.739672           5.783389      3.605402  \n",
       "6      6.418518           7.679404      3.926567  "
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_opt_result = pd.DataFrame({'Sequence name': name_lst,\n",
    "                              'Original UTR': seq_lst, \n",
    "                              'Original RL': pred_mrl_ori, \n",
    "                              'Optimized UTR': opt_lst,\n",
    "                              'Optimized RL': pred_mrl_opt, \n",
    "                              'Optimized RL_50nt': pred_mrl_opt_50,\n",
    "                              'Increased RL': inc_mrl, \n",
    "                             })\n",
    "df_opt_result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aa96d712-98e5-4294-9185-ea53dba1cbd1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "deepl",
   "language": "python",
   "name": "deepl"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
